package com.itheime.homework03;

import java.io.*;

public class InputStreamTest01 {
    public static void main(String[] args){
        try {
            read1();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("======");
        try {
            read2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void read1() throws IOException {
        //TODO 一次读取一个字节
        InputStream is = new FileInputStream("D:/file01.txt");
        int b;//用于记住读取的字节
        while ((b = is.read()) != -1) {
            System.out.println((char)b);
        }
        //流使用完毕后，必须关闭，释放系统资源
        is.close();
    }
    public static void read2() throws IOException {
        //TODO 一次读取多个字节
        InputStream is = new FileInputStream("D:/file01.txt");
        //读取多个字节到字节数组中
        byte[] b = new byte[3];
        int len;
        while ((len = is.read(b)) != -1) {
            System.out.println("读取"+len+"个字符："+new String(b,0,len));
        }
        len = is.read();
        //流使用完毕后，必须关闭，释放系统资源
        System.out.println(len);
        is.close();
    }
}
